#include <iostream>
#include <cstring>
using namespace std;
// #define ONLINE_GUDGE
using PII = pair<int, int>;
#define x first 
#define y second 
const int N = 1010, M = N*N;
const int dx[] = {-2,-2,-1,-1, 1, 1, 2, 2};
const int dy[] = {-1, 1,-2, 2,-2, 2,-1, 1};

int n,m;
char g[N][N];
PII q[M];
int st[N][N]; // 判重 state


void bfs(PII start, PII end)
{
    int hh = 0, tt = 0; // 数组模拟队列
    q[0] = start;
    st[start.x][start.y] = 0;
    
    while(hh <= tt)
    {
        PII t = q[hh++];
        for(int k = 0; k < 8; k++)
        {
            int i = t.x + dx[k], j = t.y + dy[k];
            // if(i == t.x && j == t.y)    continue;
            if(i < 0 || i >= n || j < 0 || j >= m || g[i][j] == '*' || st[i][j] <= st[t.x][t.y] + 1) continue;
            st[i][j] = st[t.x][t.y] + 1;
            q[++tt] = {i,j};
        }
    }
    cout << st[end.x][end.y] << endl;
}

int main()
{

    #ifdef ONLINE_JUDGE

    #else
    freopen("./in.txt","r",stdin);
    #endif
    ios::sync_with_stdio(false);   
	cin.tie(0);

    scanf("%d%d", &m, &n);

    for(int i = 0; i < n; i++) scanf("%s", g[i]);

    PII start, end;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            if(g[i][j] == 'H')
                end = {i,j};
            else if(g[i][j] == 'K')
                start = {i,j};

            
    memset(st, 0x3f, sizeof st);
    bfs(start, end);

    return 0;
}